package random

import (
	"fmt"
	"math/rand"
	"time"
)

type Random struct {
	Type   string
	Length int8
}

func New(t string, l int8) *Random {
	rand := &Random{
		Type:   t,
		Length: l,
	}
	return rand
}

func (r Random) Build() (res string) {
	switch r.Type {
	case "allNum":
		pool := `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
		pool = shuffle(pool)
		res = pool[0:r.Length]
	}
	return
}

func shuffle(pool string) string {
	l := len(pool)
	var sn string
	rand.Seed(time.Now().Unix())
	for true {
		if l <= 0 {
			break
		}
		index := rand.Intn(l)
		l--
		sn = fmt.Sprintf("%s%s", sn, string(pool[index]))
		pool = fmt.Sprintf("%s%s", pool[0:index], pool[index+1:])
	}
	return sn
}
